{% extends "base.html" %}

{% block title %}任务列表 - CronPy Web管理界面{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="fas fa-tasks me-2"></i>定时任务管理</h2>
            <a href="{{ url_for('add_task') }}" class="btn btn-primary">
                <i class="fas fa-plus me-1"></i>添加任务
            </a>
        </div>

        <!-- 任务统计卡片 -->
        <div class="row mb-4">
            <div class="col-md-3">
                <div class="card text-white bg-primary">
                    <div class="card-body">
                        <h5 class="card-title"><i class="fas fa-tasks me-2"></i>总任务数</h5>
                        <h3 class="mb-0">{{ tasks|length }}</h3>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card text-white bg-success">
                    <div class="card-body">
                        <h5 class="card-title"><i class="fas fa-play me-2"></i>运行中</h5>
                        <h3 class="mb-0">{{ tasks|selectattr('status', 'equalto', 'running')|list|length }}</h3>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card text-white bg-warning">
                    <div class="card-body">
                        <h5 class="card-title"><i class="fas fa-clock me-2"></i>等待中</h5>
                        <h3 class="mb-0">{{ tasks|selectattr('status', 'equalto', 'pending')|list|length }}</h3>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card text-white bg-danger">
                    <div class="card-body">
                        <h5 class="card-title"><i class="fas fa-ban me-2"></i>已禁用</h5>
                        <h3 class="mb-0">{{ tasks|selectattr('enabled', 'equalto', false)|list|length }}</h3>
                    </div>
                </div>
            </div>
        </div>

        <!-- 任务列表 -->
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-list me-2"></i>任务列表</h5>
            </div>
            <div class="card-body">
                {% if tasks %}
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>任务ID</th>
                                    <th>名称</th>
                                    <th>Cron表达式</th>
                                    <th>状态</th>
                                    <th>启用</th>
                                    <th>最后运行</th>
                                    <th>下次运行</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for task in tasks %}
                                    <tr>
                                        <td><code>{{ task.task_id }}</code></td>
                                        <td>
                                            <strong>{{ task.name }}</strong>
                                            {% if task.description %}
                                                <br><small class="text-muted">{{ task.description }}</small>
                                            {% endif %}
                                        </td>
                                        <td><code>{{ task.cron_expression }}</code></td>
                                        <td>
                                            {% if task.status == 'running' %}
                                                <span class="badge bg-success"><i class="fas fa-play me-1"></i>运行中</span>
                                            {% elif task.status == 'pending' %}
                                                <span class="badge bg-warning"><i class="fas fa-clock me-1"></i>等待中</span>
                                            {% elif task.status == 'completed' %}
                                                <span class="badge bg-info"><i class="fas fa-check me-1"></i>完成</span>
                                            {% elif task.status == 'failed' %}
                                                <span class="badge bg-danger"><i class="fas fa-times me-1"></i>失败</span>
                                            {% else %}
                                                <span class="badge bg-secondary"><i class="fas fa-pause me-1"></i>{{ task.status }}</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            {% if task.enabled %}
                                                <span class="badge bg-success"><i class="fas fa-check me-1"></i>启用</span>
                                            {% else %}
                                                <span class="badge bg-secondary"><i class="fas fa-ban me-1"></i>禁用</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            {% if task.last_run_time and task.last_run_time.strftime %}
                                                {{ task.last_run_time.strftime('%Y-%m-%d %H:%M:%S') }}
                                            {% else %}
                                                <span class="text-muted">从未</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            {% if task.next_run_time and task.next_run_time.strftime %}
                                                {{ task.next_run_time.strftime('%Y-%m-%d %H:%M:%S') }}
                                            {% else %}
                                                <span class="text-muted">未启用</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <div class="btn-group btn-group-sm">
                                                <a href="{{ url_for('edit_task', task_id=task.task_id) }}" class="btn btn-outline-primary" title="编辑">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <button type="button" class="btn btn-outline-success" onclick="runTask('{{ task.task_id }}')" title="立即执行">
                                                    <i class="fas fa-play"></i>
                                                </button>
                                                <button type="button" class="btn btn-outline-warning" onclick="toggleTask('{{ task.task_id }}')" title="启用/禁用">
                                                    {% if task.enabled %}
                                                        <i class="fas fa-ban"></i>
                                                    {% else %}
                                                        <i class="fas fa-check"></i>
                                                    {% endif %}
                                                </button>
                                                <button type="button" class="btn btn-outline-danger" onclick="deleteTask('{{ task.task_id }}', '{{ task.name }}')" title="删除">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                        <h4 class="text-muted">暂无任务</h4>
                        <p class="text-muted">点击"添加任务"按钮创建您的第一个定时任务</p>
                        <a href="{{ url_for('add_task') }}" class="btn btn-primary">
                            <i class="fas fa-plus me-1"></i>添加任务
                        </a>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>确定要删除任务 "<span id="deleteTaskName"></span>" 吗？</p>
                <p class="text-danger"><small>此操作不可撤销。</small></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger" onclick="confirmDelete()">删除</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
let deleteTaskId = '';

function deleteTask(taskId, taskName) {
    deleteTaskId = taskId;
    document.getElementById('deleteTaskName').textContent = taskName;
    new bootstrap.Modal(document.getElementById('deleteModal')).show();
}

function confirmDelete() {
    const form = document.createElement('form');
    form.method = 'POST';
    form.action = `/task/${deleteTaskId}/delete`;
    // 添加时间戳参数，防止缓存
    const timestamp = new Date().getTime();
    form.action += `?_t=${timestamp}`;
    document.body.appendChild(form);
    form.submit();
}

function runTask(taskId) {
    if (confirm('确定要立即执行此任务吗？')) {
        const form = document.createElement('form');
        form.method = 'POST';
        form.action = `/task/${taskId}/run`;
        // 添加时间戳参数，防止缓存
        const timestamp = new Date().getTime();
        form.action += `?_t=${timestamp}`;
        document.body.appendChild(form);
        form.submit();
    }
}

function toggleTask(taskId) {
    const form = document.createElement('form');
    form.method = 'POST';
    form.action = `/task/${taskId}/toggle`;
    // 添加时间戳参数，防止缓存
    const timestamp = new Date().getTime();
    form.action += `?_t=${timestamp}`;
    document.body.appendChild(form);
    form.submit();
}

function startScheduler() {
    const form = document.createElement('form');
    form.method = 'POST';
    form.action = '/scheduler/start';
    // 添加时间戳参数，防止缓存
    const timestamp = new Date().getTime();
    form.action += `?_t=${timestamp}`;
    document.body.appendChild(form);
    form.submit();
}

function stopScheduler() {
    const form = document.createElement('form');
    form.method = 'POST';
    form.action = '/scheduler/stop';
    // 添加时间戳参数，防止缓存
    const timestamp = new Date().getTime();
    form.action += `?_t=${timestamp}`;
    document.body.appendChild(form);
    form.submit();
}
</script>
{% endblock %}
